package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.tb.lmt.domain.vo.TbFdxdetailVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbPreflightdetailBo;
import com.ruoyi.tb.lmt.domain.vo.TbPreflightdetailVo;
import com.ruoyi.tb.lmt.domain.TbPreflightdetail;
import com.ruoyi.tb.lmt.mapper.TbPreflightdetailMapper;
import com.ruoyi.tb.lmt.service.ITbPreflightdetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 预检详情Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-03
 */
@RequiredArgsConstructor
@Service
public class TbPreflightdetailServiceImpl implements ITbPreflightdetailService {

    private final TbPreflightdetailMapper baseMapper;

    @Autowired
    private TbPreflightdetailMapper tbPreflightdetailMapper;

    /**
     * 查询预检详情
     */
    @Override
    public TbPreflightdetailVo queryById(Long ID){
        return baseMapper.selectVoById(ID);
    }

    /**
     * 查询预检详情列表
     */
    @Override
    public TableDataInfo<TbPreflightdetailVo> queryPageList(TbPreflightdetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbPreflightdetail> lqw = buildQueryWrapper(bo);
        Page<TbPreflightdetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询预检详情列表
     */
    @Override
    public List<TbPreflightdetailVo> queryList(TbPreflightdetailBo bo) {
        LambdaQueryWrapper<TbPreflightdetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbPreflightdetail> buildQueryWrapper(TbPreflightdetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbPreflightdetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getFdxId() != null, TbPreflightdetail::getFdxId, bo.getFdxId());
        lqw.eq(bo.getWorkCategoryId() != null, TbPreflightdetail::getWorkCategoryId, bo.getWorkCategoryId());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkCategory()), TbPreflightdetail::getWorkCategory, bo.getWorkCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkCategoryRemark()), TbPreflightdetail::getWorkCategoryRemark, bo.getWorkCategoryRemark());
        lqw.like(StringUtils.isNotBlank(bo.getWorkName()), TbPreflightdetail::getWorkName, bo.getWorkName());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckReapirReqire()), TbPreflightdetail::getCheckReapirReqire, bo.getCheckReapirReqire());
        lqw.eq(bo.getStartTime() != null, TbPreflightdetail::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, TbPreflightdetail::getEndTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getFinshSituation()), TbPreflightdetail::getFinshSituation, bo.getFinshSituation());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkGroup()), TbPreflightdetail::getWorkGroup, bo.getWorkGroup());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkUser()), TbPreflightdetail::getWorkUser, bo.getWorkUser());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckUser()), TbPreflightdetail::getCheckUser, bo.getCheckUser());
        lqw.eq(bo.getCheckTime() != null, TbPreflightdetail::getCheckTime, bo.getCheckTime());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TbPreflightdetail::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkUser2()), TbPreflightdetail::getWorkUser2, bo.getWorkUser2());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkKind()), TbPreflightdetail::getWorkKind, bo.getWorkKind());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskUrl()), TbPreflightdetail::getTaskUrl, bo.getTaskUrl());
        return lqw;
    }

    /**
     * 新增预检详情
     */
    @Override
    public Boolean insertByBo(TbPreflightdetailBo bo) {
        TbPreflightdetail add = BeanUtil.toBean(bo, TbPreflightdetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setID(add.getID());
        }
        return flag;
    }

    /**
     * 修改预检详情
     */
    @Override
    public Boolean updateByBo(TbPreflightdetailBo bo) {
        TbPreflightdetail update = BeanUtil.toBean(bo, TbPreflightdetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbPreflightdetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除预检详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<TbPreflightdetailVo> selectPreflight(String id, String lmtType, String lmtNumber, String status, PageQuery pageQuery) {
        List<TbPreflightdetailVo> result = tbPreflightdetailMapper.selectPreflight(id,lmtType,lmtNumber,status);
        //分页
        if (pageQuery.getPageSize()!= null && pageQuery.getPageNum()!= null) {
            List<TbPreflightdetailVo> page = ListUtil.page(pageQuery.getPageNum() - 1, pageQuery.getPageSize(), result);
            TableDataInfo res = new TableDataInfo<>(page, result.size());
            return res;
        } else {
            return TableDataInfo.build(result);
        }

    }

    @Override
    public TbPreflightdetailVo selectPreflightById(String id) {
        return tbPreflightdetailMapper.selectPreflightById(id);
    }
}
